﻿#region MIT

//  /*The MIT License (MIT)
// 
//  Copyright 2016 lizs lizs4ever@163.com
//  
//  Permission is hereby granted, free of charge, to any person obtaining a copy
//  of this software and associated documentation files (the "Software"), to deal
//  in the Software without restriction, including without limitation the rights
//  to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
//  copies of the Software, and to permit persons to whom the Software is
//  furnished to do so, subject to the following conditions:
//  
//  The above copyright notice and this permission notice shall be included in
//  all copies or substantial portions of the Software.
//  
//  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
//  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
//  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
//  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
//  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
//  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
//  THE SOFTWARE.
//   * */

#endregion

#if NET35
using System.Linq;
#else
using System.Linq;
using System.Collections.Concurrent;

#endif

namespace mom
{
    /// <summary>
    ///     session mgr
    /// </summary>
    public class SessionMgr
    {
        private const long ExpireDeadline = 120*1000;

        private readonly ConcurrentDictionary<ushort, Session> _items = new ConcurrentDictionary<ushort, Session>();

        /// <summary>
        ///     stop
        ///     close all sessions
        /// </summary>
        public void Stop()
        {
            Clear();
        }

        /// <summary>
        ///     add session to this mgr
        /// </summary>
        /// <param name="session"></param>
        /// <returns></returns>
        public bool AddSession(Session session)
        {
            return _items.TryAdd(session.Id, session);
        }

        /// <summary>
        ///     remove session from this mgr
        ///     remark: don't close the removed session
        /// </summary>
        /// <param name="id"></param>
        /// <param name="reason"></param>
        /// <returns></returns>
        public bool RemoveSession(ushort id, SessionCloseReason reason)
        {
            Session session;
            return _items.TryRemove(id, out session);
        }

        /// <summary>
        ///     close all sessions and clear the collection
        /// </summary>
        public void Clear()
        {
            foreach (var session in _items.Select(x => x.Value))
            {
                session.Close(SessionCloseReason.Stop);
            }

            _items.Clear();
        }

        /// <summary>
        ///     close all sessions that expired(not response after a specified period)
        /// </summary>
        public void ClearExpired()
        {
            foreach (var s in _items.Select(x => x.Value).Where(x => x.ElapsedSinceLastResponse > ExpireDeadline))
            {
                s.Close(SessionCloseReason.Stop);
            }
        }
    }
}
